package com.supermap.business.disaster;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dubbo.api.disaster.entity.Disaster;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.disaster.DisasterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author:langhongshuai
 * @Description:灾情接口层
 * @Date:18/1/29 下午2:37
 */
@Controller
@RequestMapping("DisasterController")
public class DisasterController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterController.class);

    @Reference
    private DisasterService disasterService;

    /**
     * 通过用户id和区域code获得用户的灾情信息
     */
    @RequestMapping("/getDisasterByUIdArea")
    @ResponseBody
    public JSONUtil getDisasterByUIdArea(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String areaCode = request.getParameter("areaCode");
        return disasterService.getDisasterByUIdArea(userId, areaCode);
    }

    /**
     * 添加灾情
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/addDisaster", method = RequestMethod.POST)
    @ResponseBody
    public JSONUtil addDisaster(@RequestBody HashMap<String, String> data) {
        String userId = data.get("userId");
        String farmlandId = data.get("farmlandId");
        String type = data.get("type");
        String breed = data.get("breed");
        String growthPeriod = data.get("growthPeriod");
        String longitude = data.get("longitude");
        String latitude = data.get("latitude");
        String areaCode = data.get("areaCode");
        String descript = data.get("descript");
        String imgUrl = data.get("imgUrl");
        String disasterType = data.get("disasterType");
        String development = data.get("development");

        Disaster disaster = new Disaster();

        disaster.setUserId(userId);
        disaster.setFarm(farmlandId);
        disaster.setType(type);
        disaster.setBreed(breed);
        disaster.setGrowthperiod(growthPeriod);
        disaster.setLatitude(latitude);
        disaster.setLongitude(longitude);
        disaster.setAreacode(areaCode);
        disaster.setDescript(descript);
        disaster.setImgUrl(imgUrl);
        disaster.setDisastertype(disasterType);
        disaster.setDevelopment(development);
        return disasterService.addDisaster(disaster);
    }

    /**
     * 获得所有的灾情信息
     *
     * @return
     */
    @RequestMapping("/getAllDisaster")
    @ResponseBody
    public JSONUtil getAllDisaster() {
        return disasterService.getAllDisaster();
    }

    /**
     * 删除灾情
     * @param request
     * @return
     */
    @RequestMapping("/delDisaster")
    @ResponseBody
    public JSONUtil delDisaster(HttpServletRequest request){
        String disasterId = request.getParameter("disasterId");
        return disasterService.delDisaster(Integer.parseInt(disasterId));
    }

    /**
     * 根据区域编码得到灾情
     * @param request
     * @return
     */
    @RequestMapping("/getDisasterByareacode")
    @ResponseBody
    public JSONUtil getDisasterByareacode(HttpServletRequest request){
        String areacode = request.getParameter("areacode");
        return disasterService.getDisasterByareacode(areacode);
    }

    /**
     * 获得灾情
     */
    @RequestMapping("/getDisasters")
    @ResponseBody
    public JSONUtil getDisasters(HttpServletRequest request){
        String type = request.getParameter("cropid");
        String areaCode = request.getParameter("areaCode");
        String disasterType = request.getParameter("disasterType");
        String dateTime = request.getParameter("dateTime");
        return disasterService.getDisasters(type,areaCode,disasterType,dateTime);
    }
}
